package where2go.sctu.com.zhiyou.adpater;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;

import java.util.ArrayList;
import java.util.List;

import where2go.sctu.com.zhiyou.MyApplication;
import where2go.sctu.com.zhiyou.R;
import where2go.sctu.com.zhiyou.db.ClassifyViewHolder;
import where2go.sctu.com.zhiyou.entity.Classify;

/**
 * ProjectName: zhiyou
 * PackageName: where2go.sctu.com.zhiyou.adpater
 * Created by 张某人
 * CreatedTime:on 2018/5/16   14:57
 * Description:
 */

public class RecyclerViewClassifyAdapter extends RecyclerView.Adapter<ClassifyViewHolder> {

    private String urlPersonal="http://47.104.80.155/youyiyou/resources/classImg/";
    private List<Classify.ClassifyData> dataList=new ArrayList<Classify.ClassifyData>();
    private Context context;

    public interface OnItemClickListener{
        void onItemClick(View view,int position);
        void onItemLongClick(View view,int position);
    }

    private RecyclerViewClassifyAdapter.OnItemClickListener mOnItemClickListener;

    public void setItemClickListener(RecyclerViewClassifyAdapter.OnItemClickListener listener) {
        this.mOnItemClickListener=listener;
    }
    public RecyclerViewClassifyAdapter(List<Classify.ClassifyData> dataList, Context context) {
        this.dataList = dataList;
        this.context = context;
    }

    @Override
    public ClassifyViewHolder onCreateViewHolder( ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.item_classify,parent,false);
        ClassifyViewHolder holder=new ClassifyViewHolder(view);
        return holder;
    }


    @Override
    public void onBindViewHolder(final ClassifyViewHolder holder, final int position) {
            Classify.ClassifyData classifyData=dataList.get(position);
            holder.tvTitle.setText(classifyData.getClassTitle());
            holder.tvContent.setText(classifyData.getClassDetail());
        ImageRequest imageRequest = new ImageRequest(urlPersonal+dataList.get(position).getClassImg(),
                new Response.Listener<Bitmap>() {
                    @Override
                    public void onResponse(Bitmap bitmap) {
                        holder.imgClassify.setImageBitmap(bitmap);
                    }
                }, 0, 0, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //请求失败 设置默认图片
                holder.imgClassify.setImageResource(R.mipmap.ic_launcher);
            }

        });
        MyApplication.getHttpQueues().add(imageRequest);

        if (mOnItemClickListener!=null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onItemClick(holder.itemView,position);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    mOnItemClickListener.onItemLongClick(holder.itemView,position);
                    return false;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return dataList !=null ? dataList.size():0;
    }
}
